package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitMqConfig {


      @Bean
      public Queue orderDelayQueue(){
          Map<String,Object> argument=new HashMap<>();
          // 给队列 设置 死信相关参数
          argument.put("x-dead-letter-exchange","order-event-exchange");  //消息过期 会去 路由的交换机
          argument.put("x-dead-letter-routing-key","order.release.order");  // 路由键
          argument.put("x-message-ttl",60000);
          return  new Queue("order.delay.queue",true,false,false,argument);
     }


    @Bean
    public Queue orderReleaseQueue(){
           return new Queue("order.release.order.queue",true,false,false,null);
    }


     @Bean
     public Exchange orderEventExchange(){
          return  new TopicExchange("order-event-exchange",true,false,null);
    }



    @Bean
    public Binding orderCreateBinding() {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseBinding() {

        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单服务一消费完  就发送消息给 处理解锁库存 队列 所绑定的队列
     * @return
     */

    @Bean
    public Binding orderReleaseOrderBinding() {

        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order.#",
                null);
    }




}
